/* vim:set noet ts=4: */
/** 
 * scim-python
 * 
 * Copyright (c) 2007-2008 Huang Peng <shawn.p.huang@gmail.com>
 *
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA  02111-1307  USA
 *
 * $Id: $
 */

#include "scim-python.h"
#include <glib.h>

using namespace scim;

struct PyHelperAgentObject {
	PyListObject list;
	/* Type-specific fields go here. */
	PyHelperAgent helper_agent;
};

PyHelperAgent::PyHelperAgent (PyObject *self)
	:self ((PyHelperAgentObject *)self)
{
	Py_INCREF (self);
};

PyHelperAgent::~PyHelperAgent () 
{
	Py_XDECREF ((PyObject *)self);
}

void
PyHelperAgent::operator delete (void *p)
{
	// do nothing
}

PyObject *
PyHelperAgent::py_open_connection (PyHelperAgentObject *self, PyObject *args)
{
	char *uuid = NULL;
	char *name = NULL;
	char *icon = NULL;
	char *desc = NULL;
	int opt = 0;
	char *display;
	int retval;

	if (!PyArg_ParseTuple (args, "(ssssi)s:open_connection", &uuid, &name, &icon, &desc, &opt, &display))
			return NULL;
	
	retval = self->helper_agent.open_connection (HelperInfo (uuid, name, icon, desc, opt), display);

	return PyInt_FromLong (retval);	
}

PyObject *
PyHelperAgent::py_close_connection (PyHelperAgentObject *self, PyObject *args)
{
	self->helper_agent.close_connection ();

	Py_INCREF (Py_None);
	return Py_None;	
}

PyObject *
PyHelperAgent::py_get_connection_number (PyHelperAgentObject *self, PyObject *args)
{
	int retval;

	retval = self->helper_agent.get_connection_number ();

	return PyInt_FromLong (retval);	
}

PyObject *
PyHelperAgent::py_is_connected (PyHelperAgentObject *self, PyObject *args)
{
	PyObject *result = NULL;
	bool retval;

	retval = self->helper_agent.is_connected ();
	
	if (retval) {
		result = Py_True;
	}
	else {
		result = Py_False;
	}
	Py_INCREF (result);
	return result;	
}

PyObject *
PyHelperAgent::py_has_pending_event (PyHelperAgentObject *self, PyObject *args)
{
	PyObject *result = NULL;
	bool retval;

	retval = self->helper_agent.has_pending_event ();
	
	if (retval) {
		result = Py_True;
	}
	else {
		result = Py_False;
	}
	Py_INCREF (result);
	return result;	
}

PyObject *
PyHelperAgent::py_filter_event (PyHelperAgentObject *self, PyObject *args)
{
	PyObject *result = NULL;
	bool retval;

	retval = self->helper_agent.filter_event ();
	
	if (retval) {
		result = Py_True;
	}
	else {
		result = Py_False;
	}
	Py_INCREF (result);
	return result;	
}

PyObject *
PyHelperAgent::py_reload_config (PyHelperAgentObject *self, PyObject *args)
{
	self->helper_agent.reload_config ();
	
	Py_INCREF (Py_None);
	return Py_None;	
}


PyMethodDef 
PyHelperAgent::py_methods[] = { 
		{	"open_connection", (PyCFunction)PyHelperAgent::py_open_connection, METH_VARARGS,
			"open a connection"
		},
		{	"get_connection_number", (PyCFunction)PyHelperAgent::py_get_connection_number, METH_NOARGS,
			"return id, that was returned by open_connection previously."
		},
		{	"close_connection", (PyCFunction)PyHelperAgent::py_close_connection, METH_NOARGS,
			"close the connection"
		},
		{	"is_connected", (PyCFunction)PyHelperAgent::py_is_connected, METH_NOARGS,
			"If agent is connected, the function returns True"
		},
		{	"has_pending_event", (PyCFunction)PyHelperAgent::py_has_pending_event, METH_NOARGS,
			"If agent has pending event, the function returns True"
		},
		{	"filter_event", (PyCFunction)PyHelperAgent::py_filter_event, METH_NOARGS,
			"filter events"
		},
		{	"reload_config", (PyCFunction)PyHelperAgent::py_reload_config, METH_NOARGS,
			"This function will cause all IM engines to reload config"
		},
		{ NULL }
};

PyObject *
PyHelperAgent::py_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
{
	PyHelperAgentObject *self;

	self = (PyHelperAgentObject *)type->tp_alloc (type, 0);
	return (PyObject *)self;
}

int
PyHelperAgent::py_init (PyHelperAgentObject *self, PyObject *args, PyObject *kwds)
{
	new (&self->helper_agent) PyHelperAgent ((PyObject *)self);

	return 0;
}

void
PyHelperAgent::py_dealloc (PyHelperAgentObject *self)
{
	self->helper_agent.~PyHelperAgent ();
	((PyObject *) self)->ob_type->tp_free (self);
}


HelperAgent *
PyHelperAgent::from_pyobject (PyObject *object)
{
	PyHelperAgentObject *self = (PyHelperAgentObject *) object;
	return (HelperAgent *)&self->helper_agent;
}

PyTypeObject PyHelperAgentType = {
	PyObject_HEAD_INIT (NULL)
	0,						 							/*ob_size*/
	"scim.HelperAgent", 							/*tp_name*/
	sizeof (PyHelperAgentObject),						/*tp_basicsize*/
	0,						 							/*tp_itemsize*/
	(destructor)PyHelperAgent::py_dealloc,	/*tp_dealloc*/
	0,			  										/*tp_print*/
	0,						 							/*tp_getattr*/
	0,													/*tp_setattr*/
	0,													/*tp_compare*/
	0,									/*tp_repr*/
	0,									/*tp_as_number*/
	0,									/*tp_as_sequence*/
	0,									/*tp_as_mapping*/
	0,									/*tp_hash */
	0,									/*tp_call*/
	0,		  							/*tp_str*/
	0,					   				/*tp_getattro*/
	0,									/*tp_setattro*/
	0,					 				/*tp_as_buffer*/
	Py_TPFLAGS_DEFAULT | 
	Py_TPFLAGS_BASETYPE,				/*tp_flags*/
	"HelperAgent objects",		/* tp_doc */
	0,					   /* tp_traverse */
	0,					   /* tp_clear */
	0,					   /* tp_richcompare */
	0,					   /* tp_weaklistoffset */
	0,					   /* tp_iter */
	0,					   /* tp_iternext */
	PyHelperAgent::py_methods,			 /* tp_methods */
	0,			 /* tp_members */
	0,						 /* tp_getset */
	0,						 /* tp_base */
	0,						 /* tp_dict */
	0,						 /* tp_descr_get */
	0,						 /* tp_descr_set */
	0,						 /* tp_dictoffset */
	(initproc)PyHelperAgent::py_init,	  /* tp_init */
	0,						 /* tp_alloc */
	PyHelperAgent::py_new,				 /* tp_new */	
};


static void
setint (PyObject *d, const char *name, long value)
{
	PyObject *o = PyInt_FromLong (value);
	if (o && PyDict_SetItemString (d, name, o) == 0) {
		Py_DECREF (o);
	}
}

void init_helper (PyObject *module)
{
	PyObject *dict = NULL;

	if (PyType_Ready (&PyHelperAgentType) < 0)
		return;

	Py_INCREF (&PyHelperAgentType);
	PyModule_AddObject (module, "HelperAgent", (PyObject *)&PyHelperAgentType);	
	
	dict = PyModule_GetDict (module);
	setint (dict, "SCIM_HELPER_STAND_ALONE", SCIM_HELPER_STAND_ALONE);
	setint (dict, "SCIM_HELPER_AUTO_START", SCIM_HELPER_AUTO_START);
	setint (dict, "SCIM_HELPER_AUTO_RESTART", SCIM_HELPER_AUTO_RESTART);
	setint (dict, "SCIM_HELPER_NEED_SCREEN_INFO", SCIM_HELPER_NEED_SCREEN_INFO);
	setint (dict, "SCIM_HELPER_NEED_SPOT_LOCATION_INFO", SCIM_HELPER_NEED_SPOT_LOCATION_INFO);
}
